package com.dragon.www.haier.popupwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dragon.www.haier.adapter.AppArrayAdapter;
import com.dragon.www.haier.entity.Device;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.entity.InitializeAirInfo;
import com.dragon.www.haier.entity.InitializeAirValueInfo;
import com.dragon.www.haier.entity.InitializeRelayInfo;
import com.dragon.www.haier.entity.InitializeRelayValueInfo;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 51268 on 2016/3/27.
 */
public abstract class BasePopupWindow extends PopupWindow {
    private int roomid;
    protected AppArrayAdapter mAdapter;
    protected GridView gview;
    protected Context context;
    protected View contentView;
    protected GradientDrawable myGrad;
    protected int backgroundColor=0x00000000;
    protected List<Device> devicesList;
    protected List<DeviceInfo> deviceinfoList;
    private InitializeRelayValueInfo[] result;


    public void setRoomId(int roomId) {
        this.roomid = roomId;
    }

    public int  getRoomId(){
        return roomid;
    }

    public void setDevices(List<Device> devices){
        this.devicesList = devices;
    }

    public List<Device> getDevices(){
        return this.devicesList;
    }

    public void setValues(List<DeviceInfo> deviceinfos){
        this.deviceinfoList = deviceinfos;
    }

    public BasePopupWindow(){
        super();
    }

    public BasePopupWindow(Context context){
        super(context);
        this.context = context;
    }

    public BasePopupWindow(View contentView,int widthPx, int heightPx ){
        super(contentView, widthPx, heightPx);
    }

    public void showPopWindow(View view, int xOffsetToView, int yOffsetToView){
        if(contentView!=null){
            //填充布局
            this.setContentView(contentView);
            //防黑边
            this.setBackgroundDrawable(new BitmapDrawable());
            myGrad = (GradientDrawable) contentView.getBackground();
            myGrad.setColor(backgroundColor);
        }
        super.showAsDropDown(view, xOffsetToView, yOffsetToView);
    }

    public void setHeight(int mheight) {
        super.setHeight(mheight);
    }

    public void setWidth(int mwidth){
        super.setWidth(mwidth);
    }

    public List<Device>  getData(String type) {
        List<Device> devices = new ArrayList<>();
        if( devicesList != null ){
            for( int i= 0; i < devicesList.size(); i++ ){
                String typeDev = devicesList.get(i).getType();
                if(typeDev!= null && typeDev.equals(type))
                    devices.add(devicesList.get(i));
            }
        }
        return devices;
    }
    public void getValue(String type) {
        if (type.equals("relay")){
            NetHelper.getTypeDevices(context, roomid, type, new NetUtils.NetCallBack<DeviceInfo>() {

                @Override
                public void success(DeviceInfo rspData) {
                    if (rspData.getError() == 0) {
                        final InitializeRelayInfo deviceListInfo = (InitializeRelayInfo) rspData;
                        InitializeRelayValueInfo[] result = deviceListInfo.getRelayInfo();
                        mAdapter.setValue(result);
                        Log.e("请求成功", "初始化成功");
                    } else {
                        String DD = rspData.getDescription();
                        Log.e("错误描述", DD);
                    }
                }

                @Override
                public void failed(String msg) {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }
            }, InitializeRelayInfo.class);
        }else if(type.equals("aircondition")){
            NetHelper.getTypeDevices(context, roomid, type, new NetUtils.NetCallBack<DeviceInfo>() {

                @Override
                public void success(DeviceInfo rspData) {
                    if (rspData.getError() == 0) {
                        final InitializeAirInfo deviceListInfo = (InitializeAirInfo) rspData;
                        InitializeAirValueInfo[] result = deviceListInfo.getAirInfo();
                        mAdapter.setValue(result);
                        Log.e("请求成功", "初始化成功");
                    } else {
                        String DD = rspData.getDescription();
                        Log.e("错误描述", DD);
                    }
                }

                @Override
                public void failed(String msg) {
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }
            }, InitializeAirInfo.class);
        }
    }
}
